package com.woniuxy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.annotation.MultiRequestBody;
import com.woniuxy.resultCode.Result;
import com.woniuxy.resultCode.ResultCode;
import com.woniuxy.system.entity.AssessmentType;
import com.woniuxy.system.service.AssessmentTypeService;
import com.woniuxy.system.view.AssessmentTypeView;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 考核种类	 前端控制器
 * </p>
 *
 * @author woniuxy
 * @since 2022-06-01
 */
@RestController
@RequestMapping("/system/assessmentType")
@Slf4j
@RequiresAuthentication
//@RequiresAuthentication
public class AssessmentTypeController {
    @Autowired
    AssessmentTypeService assessmentTypeService;

    @ApiOperation("获取所有考核种类")
    @PostMapping("/list")
    public Result list(@MultiRequestBody(required = false)
                                   AssessmentTypeView assessmentTypeView
                                    ,@MultiRequestBody(required = false)  String userid){
        log.info("获取参数userid:{}",userid);
        if(assessmentTypeView.getCurrentPage()==null&&assessmentTypeView.getPageSize()==null){
            List<AssessmentType> list = assessmentTypeService.list();
            return  Result.ok().data("msg",list);
        }
        QueryWrapper<AssessmentType> wrapper = getWrapper(assessmentTypeView);
        Page<AssessmentType> page=new Page<>(assessmentTypeView.getCurrentPage(),assessmentTypeView.getPageSize());
        Page<AssessmentType> result = assessmentTypeService.getAllAssessmentType(page, wrapper);
        List<AssessmentType> records = result.getRecords();
        long total = result.getTotal();
        long totalPages = result.getPages();
//        List<AssessmentType> list = assessmentTypeService.list(wrapper);
        log.info("返回数据:total{},totalPages{}",total,totalPages);
        return  Result.ok().data("msg",records).data("total",total).data("totalPages",totalPages);
    }

    @ApiOperation("新增考核种类")
    @PostMapping("/add")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result add(@MultiRequestBody AssessmentType assessmentType,@MultiRequestBody(required = false)  String userid){
        boolean flag = assessmentTypeService.addAssessmentType(assessmentType,userid);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @ApiOperation("通过id删除考核种类，restful风格")
    @DeleteMapping("/delete/{id}")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result deleteById(@PathVariable Long id){
        boolean flag = assessmentTypeService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.error()
                    .code(ResultCode.DELETE_ERROR
                            .getCode())
                                    .message(ResultCode.DELETE_ERROR
                                           .getMessage());
        }
    }

    @ApiOperation("更新考核种类")
    @PostMapping("/update")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result update(@RequestBody AssessmentType assessmentType){
        boolean flag = assessmentTypeService.updateById(assessmentType);
        if (flag){
            return  Result.ok();
        }else {
            return Result.error();
        }
    }
    //封装queryWrapper
    private QueryWrapper<AssessmentType> getWrapper(AssessmentTypeView assessmentTypeView){
        QueryWrapper<AssessmentType> queryWrapper=new QueryWrapper<>();
        if(assessmentTypeView!=null){
            if(StringUtils.isNotBlank(assessmentTypeView.getName())){
                queryWrapper.like("t.name",assessmentTypeView.getName()) ;}
            if(StringUtils.isNotBlank(assessmentTypeView.getDescription())){
                queryWrapper.like("t.description",assessmentTypeView.getDescription()) ; }
            if(StringUtils.isNotBlank(assessmentTypeView.getCreator())){
                queryWrapper.like("u.name",assessmentTypeView.getCreator()) ; }
        }
        return queryWrapper;
    }
}


